import {
	baseUrl
} from './http.js'
import {
	getStorage,
} from '@/utils/setStorage.js';
export const picturUp = (upload, url, data) => {
	let ajaxTimes = 0;
	let ajaxTimes2 = 0;
	return new Promise((resolve, reject) => {
		ajaxTimes++;
		ajaxTimes2++;
		uni.showLoading({
			title: "Uploading",
			mask: true,
		});
		// let off = setTimeout(() => {
		// 	uni.hideLoading();
		// }, 5000)
		let token = getStorage('token') || ''
		let uploadUrl = baseUrl + upload
		uni.uploadFile({
			url: uploadUrl, //服务器地址
			header: {
				// 'content-type': 'multipart/form-data',
				"x-ctwcms-token": token
			},
			filePath: url, // 图片路径
			name: 'file', // 传参形式
			formData: {
				type: data
			},
			success: uploadFileRes => {
				if (uploadFileRes.statusCode !== 200) {
					return uni.showToast({
						title: 'Upload failed',
						icon: 'none',
						position: 'top'
					});
				} else {
					const imgUrl = JSON.parse(uploadFileRes.data);
					resolve(imgUrl.data)
				}

			},
			fail: err => {
				uni.showToast({
					title: 'Upload failed',
					icon: 'none',
					position: 'top'
				});
				ajaxTimes2--;
				if (ajaxTimes2 === 0) {
					//  关闭正在等待的图标
					uni.hideToast()
				}
				reject(err)
			},
			// 完成之后关闭加载效果
			complete: () => {
				ajaxTimes--;
				if (ajaxTimes === 0) {
					//  关闭正在等待的图标
					uni.hideLoading();
				}
			}
		});
	})

}
